package com.miguo.framework.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("deprecation")
@SuppressLint("deprecation")
public class AppFramework {

    public static String DefaultUrlParam = "";

    public static Map<String, String> DefaultParam;

    private static AppFramework instance;

    private static Context mContext;

    public static AppFramework init(Context context) {
        if (instance == null) {
            instance = new AppFramework(context);
        }
        return instance;
    }

    private AppFramework(Context context) {
        mContext = context;
        initSystemInfo();
    }

    private void initSystemInfo() {

        DefaultParam = new HashMap<String, String>();
        // 手机品牌
        String phoneBrand = android.os.Build.BRAND;
        DefaultParam.put("phone_brand", phoneBrand);
        // 手机型号
        String phoneModel = android.os.Build.MODEL;
        DefaultParam.put("phone_model", phoneModel);
        // SDK版本
        String systemSDK = android.os.Build.VERSION.SDK;
        DefaultParam.put("system_sdk", systemSDK);
        // 系统版本
        String systemRelease = android.os.Build.VERSION.RELEASE;
        DefaultParam.put("system_release", systemRelease);
        String device_id = Settings.Secure.getString(mContext.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        DefaultParam.put("device_id", device_id);
        try {

            TelephonyManager mTelManager = (TelephonyManager) mContext
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String mImei = mTelManager.getDeviceId();
            DefaultParam.put("imei", mImei);
            String mImsi = mTelManager.getSubscriberId();
            DefaultParam.put("imsi", mImsi);

            WindowManager wm = (WindowManager) getContext().getSystemService(
                    Context.WINDOW_SERVICE);
            String mWidth = "" + wm.getDefaultDisplay().getWidth();
            DefaultParam.put("screen_width", mWidth);
            String mHeight = "" + wm.getDefaultDisplay().getHeight();
            DefaultParam.put("screen_height", mHeight);

            ConnectivityManager connectivityManager = (ConnectivityManager) getContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netinfo = connectivityManager.getActiveNetworkInfo();
            String mNetwork = netinfo.getTypeName();
            DefaultParam.put("network", mNetwork);

            String mPackageName = getContext().getPackageName();
            DefaultParam.put("package_name", mPackageName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String mAppVersion;
        String mAppVersionCode;
        String mChannel;
        PackageInfo mPackageInfo;
        try {
            mPackageInfo = getContext().getPackageManager().getPackageInfo(
                    getContext().getPackageName(), 0);
            mAppVersionCode = mPackageInfo.versionCode + "";
            DefaultParam.put("app_version_code", mAppVersionCode);
            mAppVersion = mPackageInfo.versionName;
            DefaultParam.put("app_version", mAppVersion);

            mChannel = getContext().getPackageManager()
                    .getApplicationInfo(getContext().getPackageName(),
                            PackageManager.GET_META_DATA).metaData
                    .getString("InstallChannel");
            DefaultParam.put("channel", mChannel);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        DefaultParam.put("platform", "android");

        List<String> keys = new ArrayList<String>(DefaultParam.keySet());

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = DefaultParam.get(key);

            if (i == DefaultParam.size() - 1) {// 拼接时，不包括最后一个&字符
                DefaultUrlParam = DefaultUrlParam + key + "=" + value;
            } else {
                DefaultUrlParam = DefaultUrlParam + key + "=" + value + "&";
            }
        }

    }

    public static Context getContext() {
        return mContext;
    }

    public static Map<String, String> getDefaultParam() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("system_time", Long.toString(System.currentTimeMillis()));
        params.putAll(DefaultParam);
        return params;
    }

    public static String getDefaultUrlParam() {
        String params = DefaultUrlParam + "&system_time="
                + System.currentTimeMillis();
        return params;
    }

}
